/*
    This file is part of the substandard lisp implementation of Standard Lisp
    Copyright (C) 2011 Nicky Nickell

    substandard lisp is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
    builtins related to identifiers as defined in section 3.3 of the
    The Standard Lisp Report
*/

function std_compress(a, env) {
  var u = a[0];

  var id = [];
  for (var p = u; p != NIL; p = p.cdr) {
    id.push(print(p.car));
  }

  return new lisp_id(id.join(''));
}
builtins['compress'] = new builtin_function(std_compress, 1, false, [TYPE_PAIR], FTYPE_EXPR);

function std_explode(a, env) {
  var u = a[0];

  var str = print(u);
  var head = new lisp_pair(new lisp_id(str[0], env.oblist), NIL);
  var ptr = head;

  for (var i = 1; i < str.length; i++) {
    ptr.cdr = new lisp_pair(new lisp_id(str[i], env.oblist), NIL);
    ptr = ptr.cdr;
  }

  return head;
}
builtins['explode'] = new builtin_function(std_explode, 1, false, [CLASS_ATOM ^ TYPE_VECTOR], FTYPE_EXPR);

function std_gensym(a, env) {
  var gsnum = env.variables.get('*GSNUM');
  var gensym = new lisp_id('G' + print(gsnum));
  env.variables.set('*GSNUM', new lisp_integer(gsnum.value.add(1)));

  return gensym;
}
builtins['gensym'] = new builtin_function(std_gensym, 0, false, [], FTYPE_EXPR);

function std_intern(a, env) {
  var u = a[0];

  throw new Error('INTERN not implemented (yet)');
}
builtins['intern'] = new builtin_function(std_intern, 1, false, [TYPE_ID | TYPE_STRING], FTYPE_EXPR);

function std_remob(a, env) {
  var u = a[0];

  throw new Error('REMOB not implemented (yet)');
}
builtins['remob'] = new builtin_function(std_remob, 1, false, [TYPE_ID], FTYPE_EXPR);
